<template>
  <div class="table-management">
    <el-card class="table-card">
      <template #header>
        <div class="card-header">
          <span>桌台管理</span>
          <el-button type="primary" @click="handleAdd">新增桌台</el-button>
        </div>
      </template>
      
      <el-table :data="tableData" style="width: 100%" v-loading="loading">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="tableNo" label="桌台号码" width="120" />
        <el-table-column prop="capacity" label="容纳人数" width="100" />
        <el-table-column prop="location" label="位置" width="120" />
        <el-table-column prop="status" label="状态" width="120">
          <template #default="scope">
            <el-tag :type="getStatusTagType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.currentPage"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    
    <!-- 新增/编辑对话框 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="500px">
      <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
        <el-form-item label="桌台号码" prop="tableNo">
          <el-input v-model="form.tableNo" placeholder="请输入桌台号码" />
        </el-form-item>
        <el-form-item label="容纳人数" prop="capacity">
          <el-input-number v-model="form.capacity" :min="1" :max="20" />
        </el-form-item>
        <el-form-item label="位置" prop="location">
          <el-input v-model="form.location" placeholder="请输入位置信息" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option label="空闲" value="空闲" />
            <el-option label="预定" value="预定" />
            <el-option label="用餐中" value="用餐中" />
            <el-option label="清理中" value="清理中" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 模拟数据
const tableData = ref([])
const loading = ref(false)
const dialogVisible = ref(false)
const dialogTitle = ref('')
const isEdit = ref(false)

// 分页参数
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
})

// 表单数据
const form = reactive({
  id: null,
  tableNo: '',
  capacity: 4,
  location: '大厅',
  status: '空闲'
})

// 表单验证规则
const rules = {
  tableNo: [
    { required: true, message: '请输入桌台号码', trigger: 'blur' }
  ],
  capacity: [
    { required: true, message: '请输入容纳人数', trigger: 'blur' }
  ],
  location: [
    { required: true, message: '请输入位置信息', trigger: 'blur' }
  ],
  status: [
    { required: true, message: '请选择状态', trigger: 'change' }
  ]
}

// 表单引用
const formRef = ref(null)

// 获取状态标签类型
const getStatusTagType = (status) => {
  switch (status) {
    case '空闲':
      return 'success'
    case '用餐中':
      return 'danger'
    case '预定':
      return 'warning'
    case '清理中':
      return 'info'
    default:
      return ''
  }
}

// 获取状态文本
const getStatusText = (status) => {
  return status
}

// 获取桌台列表
const fetchTableList = async () => {
  loading.value = true
  try {
    // 模拟API调用
    await new Promise(resolve => setTimeout(resolve, 500))
    
    // 模拟数据
    const mockData = [
      { id: 1, tableNo: 'T001', capacity: 4, location: '大厅', status: '空闲', createTime: '2025-11-05 10:00:00' },
      { id: 2, tableNo: 'T002', capacity: 6, location: '包间', status: '预定', createTime: '2025-11-05 10:00:00' },
      { id: 3, tableNo: 'T003', capacity: 2, location: '窗边', status: '用餐中', createTime: '2025-11-05 10:00:00' },
      { id: 4, tableNo: 'T004', capacity: 4, location: '大厅', status: '清理中', createTime: '2025-11-05 10:00:00' }
    ]
    
    tableData.value = mockData
    pagination.total = mockData.length
  } catch (error) {
    ElMessage.error('获取桌台列表失败')
  } finally {
    loading.value = false
  }
}

// 处理新增
const handleAdd = () => {
  dialogTitle.value = '新增桌台'
  isEdit.value = false
  resetForm()
  dialogVisible.value = true
}

// 处理编辑
const handleEdit = (row) => {
  dialogTitle.value = '编辑桌台'
  isEdit.value = true
  Object.assign(form, row)
  dialogVisible.value = true
}

// 处理删除
const handleDelete = (row) => {
  ElMessageBox.confirm('确定要删除该桌台吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    // 模拟删除操作
    ElMessage.success('删除成功')
    fetchTableList()
  }).catch(() => {
    // 取消删除
  })
}

// 重置表单
const resetForm = () => {
  Object.assign(form, {
    id: null,
    tableNo: '',
    capacity: 4,
    location: '大厅',
    status: '空闲'
  })
}

// 处理提交
const handleSubmit = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      if (isEdit.value) {
        // 模拟编辑操作
        ElMessage.success('编辑成功')
      } else {
        // 模拟新增操作
        ElMessage.success('新增成功')
      }
      dialogVisible.value = false
      fetchTableList()
    }
  })
}

// 处理分页大小变化
const handleSizeChange = (val) => {
  pagination.pageSize = val
  fetchTableList()
}

// 处理当前页变化
const handleCurrentChange = (val) => {
  pagination.currentPage = val
  fetchTableList()
}

// 页面加载时获取数据
onMounted(() => {
  fetchTableList()
})
</script>

<style scoped>
.table-management {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
}
</style>